/**
 * 日期格式化工具
 */

// 格式化日期为 YYYY-MM-DD HH:MM:SS
export function formatDate(date, includeTime = true) {
    if (!date) return '';

    const d = new Date(date);

    const year = d.getFullYear();
    const month = String(d.getMonth() + 1).padStart(2, '0');
    const day = String(d.getDate()).padStart(2, '0');

    if (!includeTime) {
        return `${year}-${month}-${day}`;
    }

    const hours = String(d.getHours()).padStart(2, '0');
    const minutes = String(d.getMinutes()).padStart(2, '0');
    const seconds = String(d.getSeconds()).padStart(2, '0');

    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

// 格式化状态文本
export function formatStatus(status) {
    const statusMap = {
        'pending': '待发布',
        'active': '进行中',
        'completed': '已结束',
        'cancelled': '已取消'
    };
    return statusMap[status] || status;
}

// 获取状态对应的Tag类型
export function getStatusType(status) {
    const typeMap = {
        'pending': 'info',
        'active': 'success',
        'completed': 'primary',
        'cancelled': 'danger'
    };
    return typeMap[status] || 'info';
}

// 格式化数字
export function formatNumber(num) {
    return num.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
}

// 格式化时间段（将秒转为可读时间）
export function formatDuration(seconds) {
    if (!seconds || seconds < 0) return '0秒';

    const days = Math.floor(seconds / (24 * 60 * 60));
    seconds = seconds % (24 * 60 * 60);

    const hours = Math.floor(seconds / (60 * 60));
    seconds = seconds % (60 * 60);

    const minutes = Math.floor(seconds / 60);
    seconds = seconds % 60;

    let result = '';
    if (days > 0) result += `${days}天`;
    if (hours > 0) result += `${hours}小时`;
    if (minutes > 0) result += `${minutes}分钟`;
    if (seconds > 0) result += `${seconds}秒`;

    return result;
} 